
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException, ElementNotVisibleException
from urllib.parse import urlencode
import time
import random

is_login=False

options = webdriver.FirefoxOptions()
options.set_headless()
options.add_argument('--disable-gpu')
driver=webdriver.Firefox(firefox_options=options)
#driver = webdriver.Firefox()

driver.get("http://www.baidu.com")

#find_element_by_id
#find_element_by_name
#find_element_by_xpath
#find_element_by_link_text
#find_element_by_partial_link_text
#find_element_by_tag_name
#find_element_by_class_name
#find_element_by_css_selector
#time.sleep(2)
#for i in range(2):
#    elem=WebDriverWait(driver, 2).until(
#        lambda x:x.find_element_by_xpath('//*[@id="header"]/div[2]/div/label[2]/input'))
#
#    elem.send_keys("5")
#    time.sleep(5)
#    print ("send a 5",i)

c0=10
bonus=2.2
stage=-1
price=[c0*(2**x) for x in range(0,11)]
round_cnt=miss_cnt=0
total_pionts=org_pionts=5000.0
 
if is_login:
    time.sleep(30)
else:
    time.sleep(5)
    
    elem=driver.find_element_by_xpath('//*[@id="login_form"]/div/ul/div[6]/button')
    elem.click()
    
    time.sleep(2)
    alert = driver.switch_to_alert()
    alert.accept()

time.sleep(2)
elem=driver.find_element_by_id('l_mspk10')
elem.click()

print ("GAME Started !!!")

max_stage=8
pre_score=1e9*1.0

while True:

    time.sleep(10)
    round_cnt = round_cnt + 1

    driver.switch_to.default_content()
    if is_login:
        elem=driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[1]/div[2]/div[3]/span')
    else:
        elem=driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[1]/div[2]/div[2]/span')

    score = float(elem.text)
    if pre_score <= score:
        total_pionts = total_pionts + price[stage]*bonus
        stage = 0
        total_pionts = total_pionts - price[stage]
        print ("H [{}]round{:8} {:8} {:8} {:.2f} BLC[{:.2f}]".format(time.strftime('%H:%M:%S',time.localtime()),round_cnt,stage,price[stage],total_pionts,score))
    else:
        stage = 0 if stage > max_stage else stage + 1
        if total_pionts >= price[stage]:
        	total_pionts = total_pionts - price[stage]
        	print ("M [{}]round{:8} {:8} {:8} {:.2f} BLC[{:.2f}]".format(time.strftime('%H:%M:%S',time.localtime()),round_cnt,stage,price[stage],total_pionts,score))
        else:
        	break

    pre_score = score

    driver.switch_to_frame("frame")
    time.sleep(2)
    
    for i in range(20):
        elem=driver.find_element_by_xpath('//*[@id="cdClose"]')
        ts = int(elem.text[3:5])
        if 10 < ts < 50:
            break
        else:
            time.sleep(7)
    
    if True:
        elem=driver.find_element_by_name("gysum_big")
        elem.clear()
        time.sleep(1)
        elem.send_keys(price[stage])
    
    if True:
        time.sleep(1)
        elem=driver.find_element_by_xpath("/html/body/div/div[1]/div[2]/div/input[1]")
        elem.click()
    
        time.sleep(1)
        driver.switch_to.default_content()
        elem=driver.find_element_by_xpath("/html/body/div[6]/div[3]/div/button[1]")
        elem.click()

    driver.switch_to_frame("frame")
    time.sleep(1)

    for i in range(20):
        elem=driver.find_element_by_xpath('//*[@id="cdClose"]')
        #print ("    " + elem.text)
        ts = int(elem.text[3:5])
        if ts == 0:
            break
        else:
            time.sleep(7)

    if total_pionts >= 2*org_pionts or round_cnt > 10000:
        break

    time.sleep(random.randint(22,35))

driver.close()
